package com.liuhuanss.voiceprint.controller;

import com.liuhuanss.voiceprint.config.VoiceprintProperties;
import com.liuhuanss.voiceprint.dto.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置管理控制器
 * 提供系统配置的查看和修改功能
 */
@RestController
@RequestMapping("/api/config")
@CrossOrigin(origins = "*")
public class ConfigController {

    private static final Logger log = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private VoiceprintProperties voiceprintProperties;

    /**
     * 获取当前声纹识别配置
     */
    @GetMapping("/voiceprint")
    public ApiResponse<Map<String, Object>> getVoiceprintConfig() {
        try {
            Map<String, Object> config = new HashMap<>();
            config.put("similarityThreshold", voiceprintProperties.getRecognition().getSimilarityThreshold());
            config.put("voiceprintThreshold", voiceprintProperties.getRecognition().getVoiceprintThreshold());
            config.put("maxSize", voiceprintProperties.getAudio().getMaxSize());
            config.put("allowedFormats", voiceprintProperties.getAudio().getAllowedFormats());
            config.put("sampleRate", voiceprintProperties.getAudio().getSampleRate());
            
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("获取声纹配置失败", e);
            return ApiResponse.error("获取配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新声纹识别阈值
     */
    @PostMapping("/voiceprint/threshold")
    public ApiResponse<Void> updateThreshold(@RequestBody Map<String, Double> request) {
        try {
            Double similarityThreshold = request.get("similarityThreshold");
            Double voiceprintThreshold = request.get("voiceprintThreshold");
            
            // 验证阈值范围
            if (similarityThreshold != null) {
                if (similarityThreshold < 0.0 || similarityThreshold > 1.0) {
                    return ApiResponse.error("相似度阈值必须在0.0-1.0之间");
                }
                voiceprintProperties.getRecognition().setSimilarityThreshold(similarityThreshold);
                log.info("相似度阈值已更新为: {}", similarityThreshold);
            }
            
            if (voiceprintThreshold != null) {
                if (voiceprintThreshold < 0.0 || voiceprintThreshold > 1.0) {
                    return ApiResponse.error("声纹阈值必须在0.0-1.0之间");
                }
                voiceprintProperties.getRecognition().setVoiceprintThreshold(voiceprintThreshold);
                log.info("声纹阈值已更新为: {}", voiceprintThreshold);
            }
            
            return ApiResponse.success("阈值更新成功");
        } catch (Exception e) {
            log.error("更新阈值失败", e);
            return ApiResponse.error("更新失败: " + e.getMessage());
        }
    }

    /**
     * 获取系统状态信息
     */
    @GetMapping("/system/status")
    public ApiResponse<Map<String, Object>> getSystemStatus() {
        try {
            Map<String, Object> status = new HashMap<>();
            status.put("status", "running");
            status.put("timestamp", System.currentTimeMillis());
            status.put("version", "1.0.0");
            
            // 获取JVM信息
            Runtime runtime = Runtime.getRuntime();
            Map<String, Object> memory = new HashMap<>();
            memory.put("total", runtime.totalMemory());
            memory.put("free", runtime.freeMemory());
            memory.put("used", runtime.totalMemory() - runtime.freeMemory());
            memory.put("max", runtime.maxMemory());
            status.put("memory", memory);
            
            return ApiResponse.success(status);
        } catch (Exception e) {
            log.error("获取系统状态失败", e);
            return ApiResponse.error("获取系统状态失败: " + e.getMessage());
        }
    }

    /**
     * 重置配置为默认值
     */
    @PostMapping("/voiceprint/reset")
    public ApiResponse<Void> resetConfig() {
        try {
            voiceprintProperties.getRecognition().setSimilarityThreshold(0.7);
            voiceprintProperties.getRecognition().setVoiceprintThreshold(0.6);
            
            log.info("声纹识别配置已重置为默认值");
            return ApiResponse.success("配置重置成功");
        } catch (Exception e) {
            log.error("重置配置失败", e);
            return ApiResponse.error("重置失败: " + e.getMessage());
        }
    }
}